home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / pascal / dlgdrw.exe / DLGDRAW.CPP < prev   
C/C++ Source or Header  |  1992-08-12  |  6KB  |  244 lines

  1. //========================================================================
  2. //  The following example routines have been provided by the Technical
  3. //  Support staff at Borland International.  They are provided as a
  4. //  courtesy and not as part of a Borland product, and as such, are
  5. //  provided without the assurance of technical support or any specific
  6. //  guarantees.
  7. //========================================================================
  8. //  Turbo Vision - Changing text, text color, and sending messages in
  9. //    a dialog box
  10. //
  11. //  - The MyApp::handleEvent function overrides the default handlevent
  12. //  enabling you to respond to user specific commands... in this case
  13. //  cmDrawDialog.
  14. //
  15. //  -TDialog is the base class for a new user defined class MyDialog.
  16. //
  17. //  - In MyDialog the draw member function is overloaded and allows for
  18. //  redifinition of TDialog::draw() function behavior.
  19. //
  20. //  - In the idle loop a counter is incremented and checked for a value
  21. //  via the mod operator.  When the value is found, a broadcast message
  22. //  is sent.
  23. //
  24. //  - The Mydialog::handleEvent function overrides the default handleEvent
  25. //  of TDialog enabling our dialog to respond to the broadcast command.
  26. //
  27. //------------------------------------------------------------------------
  28.  
  29.  
  30. #define Uses_TEventQueue
  31. #define Uses_TEvent
  32. #define Uses_TProgram
  33. #define Uses_TApplication
  34. #define Uses_TKeys
  35. #define Uses_TRect
  36. #define Uses_TMenuBar
  37. #define Uses_TSubMenu
  38. #define Uses_TMenuItem
  39. #define Uses_TStatusLine
  40. #define Uses_TStatusItem
  41. #define Uses_TStatusDef
  42. #define Uses_TDeskTop
  43. #define Uses_TView
  44. #define Uses_TWindow
  45. #define Uses_TFrame
  46. #define Uses_TDialog
  47. #define Uses_TButton
  48. #define Uses_TSItem
  49. #define Uses_TMenu
  50.  
  51. #include <stdlib.h>      //random
  52. #include <string.h>      //strdup, strlen
  53. #include <tv.h>
  54.  
  55. #define TIMEOUT       8000          //constant used in idle loop
  56. #define MAX_STRINGS      7          //seven strings in the array
  57. #define MAX_COLORS      16          //colors available
  58.  
  59. const int cmDrawDialog  = 100;
  60. const int cmDrawLine    = 101;
  61.  
  62.  
  63. class TMyApp : public TApplication
  64. {
  65.  
  66. private:
  67.   unsigned count;      //a count variable...just for fun
  68.  
  69. public:
  70.     TMyApp();
  71.     static TMenuBar *initMenuBar( TRect );
  72.     static TStatusLine *initStatusLine( TRect );
  73.     void handleEvent(TEvent& event);
  74. protected:
  75.     void DrawDialog(void);
  76.     void idle(void);
  77.  
  78. };
  79.  
  80.  
  81.  
  82. class myDialog : public TDialog
  83. {
  84.  
  85.     char *dialogStr;                     //pointer to current string
  86.     char *stringList[MAX_STRINGS];       //array of pointers
  87.     unsigned stringNum;                  //index into array of strings
  88.  
  89.    public:
  90.       myDialog(const TRect& bounds, const char *aTitle = "Draw Dialog"): TDialog( bounds, aTitle),
  91.      TWindowInit(&myDialog::initFrame)
  92.      {
  93.         stringNum = 0;                 //initialize to first string
  94.         stringList[0] = strdup("Example of...");
  95.         stringList[1] = strdup("catching events and...");
  96.         stringList[2] = strdup("sending messages and...");
  97.         stringList[3] = strdup("changing text...");
  98.         stringList[4] = strdup("in a dialog box.");
  99.         stringList[5] = strdup("Borland International");
  100.         stringList[6] = strdup("         1992        ");
  101.      };
  102.  
  103.       void draw();
  104.       virtual void handleEvent( TEvent& event);
  105.  
  106. };
  107.  
  108. //==========================================================================
  109. //        Overlaoded draw:
  110. //           Change the text pointed to by dialogStr, change the
  111. //           color by taking the mod of stringNum.
  112. //==========================================================================
  113. void myDialog::draw()
  114. {
  115.  
  116.     TDialog::draw();                             //call base class draw()
  117.  
  118.     char textAttr = getColor(1) & 0xF0;          //set text attributes
  119.     ushort color = stringNum % MAX_COLORS;       //set color
  120.  
  121.     TDrawBuffer b;
  122.     dialogStr = stringList[ stringNum++ % MAX_STRINGS ] ;
  123.     b.moveStr( 0, dialogStr, color + textAttr );
  124.     writeLine(14,7,strlen(dialogStr),1,b);
  125.  
  126. }
  127.  
  128. //==========================================================================
  129. //       If the event is evBroadcast and the command is cmDrawLine...
  130. //       then call the draw member function.
  131. //==========================================================================
  132. void myDialog::handleEvent( TEvent& event)
  133. {
  134.  
  135.    if ( event.what == evBroadcast )
  136.    {
  137.       switch( event.message.command )
  138.       {
  139.  
  140.         case cmDrawLine:
  141.            draw();
  142.            break;
  143.  
  144.         default:
  145.            break;
  146.       }
  147.  
  148.    }
  149.  
  150.    TDialog::handleEvent( event );
  151.  
  152. }
  153.  
  154.  
  155. void TMyApp::DrawDialog()
  156. {
  157.  
  158.     //count = 1;     //initialize the count variable
  159.     myDialog *pd = new myDialog( TRect( 15, 4, 65, 20) );
  160.  
  161.     if( validView(pd) )
  162.     {
  163.        deskTop->execView( pd );
  164.     }
  165.     destroy( pd );
  166. }
  167.  
  168.  
  169. TMyApp::TMyApp() :
  170.     TProgInit( &initStatusLine,
  171.            &initMenuBar,
  172.            &initDeskTop
  173.          )
  174. {
  175.     count = 1;
  176. }
  177.  
  178.  
  179. //===========================================================================
  180. //      When count is evenly divisible by TIMEOUT... send a broadcast
  181. //      message to the deskTop.
  182. //===========================================================================
  183. void TMyApp::idle()
  184. {
  185.     TProgram::idle();
  186.     if( ! (count++ % TIMEOUT) )
  187.        message(deskTop, evBroadcast, cmDrawLine, 0);
  188.  
  189. }
  190.  
  191.  
  192.  
  193. void TMyApp::handleEvent(TEvent& event)
  194. {
  195.    TApplication::handleEvent( event );
  196.  
  197.    if( event.what == evCommand )
  198.    {
  199.       switch( event.message.command)
  200.       {
  201.    case cmDrawDialog:
  202.         DrawDialog();
  203.         break;
  204.  
  205.      default:
  206.         break;
  207.       }
  208.       clearEvent( event );
  209.    }
  210. }
  211.  
  212.  
  213.  
  214. TStatusLine *TMyApp::initStatusLine(TRect r)
  215. {
  216.    r.a.y = r.b.y - 1;
  217.  
  218.    return new TStatusLine( r,
  219.        *new TStatusDef( 0, 0xFFFF) +
  220.        *new TStatusItem( "~Alt-X~ Exit", kbAltX, cmQuit) +
  221.        *new TStatusItem( "~Alt-D~ Draw Dialog", kbAltD, cmDrawDialog)
  222.        );
  223. }
  224.  
  225.  
  226. TMenuBar *TMyApp::initMenuBar( TRect r )
  227. {
  228.     r.b.y = r.a.y + 1;
  229.  
  230.    return new TMenuBar( r,
  231.         *new TSubMenu( "~\xF0~", kbAltSpace ) +
  232.         *new TMenuItem( "~D~raw Dialog", cmDrawDialog, kbAltD, hcNoContext, "Alt-D"));
  233.  
  234.  
  235. }
  236.  
  237.  
  238. int main()
  239. {
  240.    TMyApp myApp;
  241.    myApp.run();
  242.    return 0;
  243. }
  244.